package com.meng.a_controller;

import com.meng.b_service.UserService;
import com.meng.d_model.User;
import com.meng.utils.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;


/**
 * 用户的控制层
 *
 * @author MENG
 * @version 2017/12/15
 * @see
 */
@RestController
public class UserController
{

    /**
     * 用户service层
     */
    @Autowired
    private UserService userService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;//编译报错  未解决不影响使用可能是编译器问题

    /**
     * 跳转登录页面
     *
     * @return
     */
    @RequestMapping("/")
    private ModelAndView toLogin()
    {
        return new ModelAndView("/login/login");
    }




    /**
     * 普通登录
     *
     * @return
     */
//    @RequestMapping("login")
//    private boolean login(HttpServletRequest request,String email, String password)
//        throws NoSuchAlgorithmException
//    {
//        User user = userService.getOne(email);
//
//        if(user == null)
//        {
//            return false;
//        }
//
//        String md5Pd = Utils.encodeByMd5(password);
//
//        if(user.getPassword().equals(md5Pd))
//        {
//
//            System.out.println(request.getSession().getId());
//
//            //连接Redis 将user用户放到reids共享session上
//            request.getSession().setAttribute("user",user);
//
//            return true;
//        }
//
//        return false;
//    }

    /**
     * 登录认证 shiro
     *
     * @return
     */
    @RequestMapping("login")
    private boolean login(HttpServletRequest request,String email, String password)
        throws NoSuchAlgorithmException
    {
        //shiro 登入认证
        String md5Pd = Utils.encodeByMd5(password);

        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(email,md5Pd);

        Subject subject = SecurityUtils.getSubject();

        try
        {
            //默认记录用户
            usernamePasswordToken.setRememberMe(true);

            subject.login(usernamePasswordToken);   //完成登录

            User user=(User) subject.getPrincipal();

            System.out.println(request.getSession().getId());

            //连接Redis 将user用户放到reids共享session上
            request.getSession().setAttribute("user",user);

            return true;

        } catch(Exception e)
        {
            return false;//登录验证错误
        }
    }

    /**
     *
     * shiro 记住我功能 未实现
     *
     * @param request
     * @return
     */
    @RequestMapping("isLogin")
    public ModelAndView isLogin(HttpServletRequest request)
    {
        //FIXME 修改shiro记住我功能
        Subject subject = SecurityUtils.getSubject();

        //认证成功 重新放入session   注：Remembered和Authenticated两者是互斥的，若一个为true，则另一个为false，反之亦然。
        System.out.println("isAuthenticated = "+subject.isAuthenticated());

        System.out.println("isRemembered = "+subject.isRemembered());

        if(subject.isRemembered())
        {
            User user=(User) subject.getPrincipal();

            System.out.println(request.getSession().getId());

            //连接Redis 将user用户放到reids共享session上
            request.getSession().setAttribute("user",user);
        }
        else
        {
            return new ModelAndView("/login/login");
        }

        String url = request.getRequestURI();

        return new ModelAndView("/common/to_request_url");
    }

    @RequestMapping("logout")
    private ModelAndView logout(HttpServletRequest request)
    {
        request.getSession().removeAttribute("user");

        Subject subject = SecurityUtils.getSubject();

        subject.logout();//shiro 登出

        return new ModelAndView("/login/login");
    }


}
